import PubSub from 'pubsub-js';
import request from '../../../../utils/request';
const appInstance = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    day: '', // 天
    month: '', // 月
    recommendList: [], // 推荐列表数据
    index: 0, // 点击音乐的下标,
    type: 'recommendSondType' //通信id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // 判断用户是否登录
    let userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'error',
        success: () => {
          // 跳转至登录界面
          wx.reLaunch({
            url: '/pages/login/login'
          })
        }
      })
    }
    // 更新日期的状态数据
    this.setData({
      day: new Date().getDate(),
      month: new Date().getMonth() + 1
    })

    // 获取每日推荐的数据
    this.getRecommendList();

    // 订阅来自songDetail页面发布的消息
    PubSub.subscribe(this.data.type, (msg, type) => {
      let {
        recommendList,
        index
      } = this.data;
      index = appInstance.globalData.currentIndex;
      // if(type === 'pre'){ // 上一首
      //   (index === 0) && (index = recommendList.length);
      //   index -= 1;
      // }else { // 下一首
      //   (index === recommendList.length - 1) && (index = -1);
      //   index += 1;
      // }

      // 更新下标
      this.setData({
        index
      })

      let musicId = recommendList[index].id;
      // 将musicId回传给songDetail页面
      PubSub.publish('musicId', musicId)

    });
  },

  // 获取用户每日推荐数据
  async getRecommendList() {
    let recommendListData = await request('/recommend/songs');
    console.log(recommendListData)
    this.setData({
      recommendList: recommendListData.data.dailySongs
    })
  },

  // 跳转至songDetail页面
  // toSongDetail(event){
  //   let that = this;
  //   let {song, index} = event.currentTarget.dataset;
  //   this.setData({
  //     index
  //   })
  //   // 路由跳转传参： query参数
  //   wx.navigateTo({
  //     // 不能直接将song对象作为参数传递，长度过长，会被自动截取掉
  //     // url: '/pages/songDetail/songDetail?songPackage=' + JSON.stringify(songPackage)
  //     url: `/pages/songPackage/pages/songDetail/songDetail?type=${this.data.type}&musicId=` + song.id,
  //     success:function(){
  //       appInstance.globalData.playList = that.data.recommendList;
  //       appInstance.globalData.currentIndex = index;
  //     }
  //   },

  //   )
  // },

  toSongDetail(event) {

    let that = this;
    let globList = [];
    let index
    if (event.currentTarget.dataset.index) {
      index = event.currentTarget.dataset.index;
    } else {
      index = 0
    }

    let {
      recommendList
    } = this.data
    let songlist = recommendList
    this.setData({
      index,
      type: `recommendSondType`
    })
    //  console.log(songlist)
    songlist.forEach((item, index) => {
      let obj = {}
      obj.name = item.name;
      obj.artists = item.ar;
      obj.id = item.id
      globList.push(obj)
    });
    // console.log(globList)

    this.setData({
      globList
    })


    appInstance.globalData.playList = that.data.globList;
    appInstance.globalData.currentIndex = index;
    console.log(index)
    // console.log(songlist[index].id)
    // 路由跳转传参： query参数
    wx.navigateTo({
        // 不能直接将song对象作为参数传递，长度过长，会被自动截取掉
        // url: '/pages/songDetail/songDetail?songPackage=' + JSON.stringify(songPackage)
        url: `/pages/songPackage/pages/playmusic/playmusic?type=${this.data.type}&musicId=` + songlist[index].id,
        // success:function(){

        // }
      },

    )
  },


  //播放全部
  playall() {
    let that = this;
    this.setData({
      index: 0
    })
    // 路由跳转传参： query参数
    wx.navigateTo({
        // 不能直接将song对象作为参数传递，长度过长，会被自动截取掉
        // url: '/pages/songDetail/songDetail?songPackage=' + JSON.stringify(songPackage)
        url: `/pages/songPackage/pages/songDetail/songDetail?type=${this.data.type}&musicId=` + this.data.recommendList[0].id,
        success: function () {
          appInstance.globalData.playList = that.data.recommendList;
          appInstance.globalData.currentIndex = that.data.index;
        }
      },

    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})